package com.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bean.dto.Dto;
import com.bean.pojo.*;
import com.bean.vo.order.*;
import com.service.*;
import com.util.DtoUtil;
import com.util.PageAssistant;
import org.dozer.DozerBeanMapper;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@RestController
public class HotelOrderController {

    @Autowired
    private IItripHotelOrderService iItripHotelOrderService;
    @Autowired
    private IItripHotelService itripHotelService;
    @Autowired
    private IItripHotelTempStoreService iItripHotelTempStoreService;
    @Autowired
    private IItripHotelRoomService iItripHotelRoomService;
    @Autowired
    private IItripUserLinkUserService itripUserLinkUserService;

    @PostMapping("/api/hotelorder/validateroomstore")
    public
    @ResponseBody
    Dto validateroomstore(@RequestBody ValidateRoomStoreVO validateRoomStoreVO) {
        System.out.println(">>>修改订房日期验证是否有房");
        System.out.println(validateRoomStoreVO);

        return null;
    }

   @GetMapping("/api/hotelorder/getpersonalorderinfo/{orderId}")
    public Dto getpersonalorderinfo(@PathVariable("orderId") String orderId) {
        System.out.println(">>> 根据订单ID查看个人订单详情");
        System.out.println("订单ID:" + orderId);

        if (orderId == null) {
            return DtoUtil.returnError("100525 ", "请传递参数：orderId", "");
        }

        QueryWrapper<ItripHotelOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("id", orderId);

       ItripHotelOrder one = new ItripHotelOrder();

       ItripPersonalHotelOrderVO itripPersonalHotelOrderVO = new ItripPersonalHotelOrderVO();
        try {
            one = iItripHotelOrderService.getOne(wrapper);
            //转换vo映射
            Mapper mapper = new DozerBeanMapper();

            itripPersonalHotelOrderVO = mapper.map(one, ItripPersonalHotelOrderVO.class);
            itripPersonalHotelOrderVO.setRoomPayType(one.getPayType());

        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnError("100527 ", "获取个人订单信息错误", "");
        }

        if (itripPersonalHotelOrderVO== null) {
            return DtoUtil.returnError("100526", "没有相关订单信息", "");
        } else {
            return DtoUtil.returnSuccess("查询成功", itripPersonalHotelOrderVO);
        }
    }

    @GetMapping("/api/hotelorder/getpersonalorderroominfo/{orderId}")
    public Dto getpersonalorderroominfo(@PathVariable("orderId") String orderId) {
        System.out.println(">>> 根据订单ID查看个人订单详情-房型相关信息ItripPersonalOrderRoomVO");
        System.out.println("订单ID:" + orderId);

        if (orderId == null) {
            return DtoUtil.returnError("100529", "请传递参数：orderId", "");
        }

        ItripPersonalOrderRoomVO ginfo = iItripHotelOrderService.getpersonalorderroominfo(Integer.parseInt(orderId));

        System.out.println("测试+++"+ginfo);

        if(ginfo!=null){
            return DtoUtil.returnSuccess("查询成功", ginfo);
        }else{
            return DtoUtil.returnError("100531 ", "获取个人订单房型信息错误", "");
        }

    }

    @PostMapping("/api/hotelorder/getpersonalorderlist")
    public
    @ResponseBody
    Dto getpersonalorderlist(@RequestBody ItripSearchOrderVO isoo,PageAssistant<ItripHotelOrder> pa) {
        System.out.println(">>> 根据个人订单列表，并分页显示");
        System.out.println(isoo);
        System.out.println("当前页"+isoo.getPageNo()+"每页个数"+isoo.getPageSize());
        //！！！！
//        isoo.setPageNo(1);
        QueryWrapper<ItripHotelOrder> wrapper = new QueryWrapper<>();
        wrapper.ge(isoo.getStartDate()!=null,"creationDate", isoo.getStartDate());
        wrapper.le(isoo.getEndDate()!=null,"creationDate", isoo.getEndDate());
        wrapper.like(isoo.getLinkUserName()!=null&&!isoo.getLinkUserName().equals(""),"linkUserName", isoo.getLinkUserName());
        wrapper.eq(isoo.getOrderNo()!=null&&!isoo.getOrderNo().equals(""),"orderNo", isoo.getOrderNo());
        wrapper.eq(isoo.getOrderStatus()!=-1,"orderStatus", isoo.getOrderStatus());
        wrapper.eq(isoo.getOrderType()!=-1,"orderType", isoo.getOrderType());

        if(isoo.getPageNo()!=null){
            IPage<ItripHotelOrder> page = new Page<>(isoo.getPageNo(), isoo.getPageSize());
        }
        IPage<ItripHotelOrder> page = new Page<>(1, isoo.getPageSize());

        pa.setRows(iItripHotelOrderService.page(page, wrapper).getRecords());
        pa.setPageSize(isoo.getPageSize());
        pa.setTotal((int) page.getTotal());
        pa.setCurPage(isoo.getPageNo());


        return DtoUtil.returnSuccess("获取个人订单列表成功", pa);
    }

    @PostMapping("/api/hotelorder/getpreorderinfo")
    public
    @ResponseBody
    Dto getpreorderinfo(@RequestBody ValidateRoomStoreVO vrsv) {
        System.out.println(">>> 生成订单前,获取预订信息");
        System.out.println(vrsv);

        if (vrsv.getHotelId() == null) {
            return DtoUtil.returnError("100510 ", "hotelId不能为空", "");
        }
        if (vrsv.getRoomId() == null) {
            return DtoUtil.returnError("100511 ", "roomId不能为空", "");
        }
        //返回RoomStoreVO
        RoomStoreVO roomStoreVO = new RoomStoreVO();
        roomStoreVO.setHotelId(vrsv.getHotelId());
        roomStoreVO.setRoomId(vrsv.getRoomId());
        roomStoreVO.setCheckInDate(vrsv.getCheckInDate());
        roomStoreVO.setCheckOutDate(vrsv.getCheckOutDate());
        roomStoreVO.setCount(vrsv.getCount());
        //获取酒店名
        QueryWrapper<ItripHotel> wrapper = new QueryWrapper<>();
        wrapper.eq("id", vrsv.getHotelId());
        roomStoreVO.setHotelName(itripHotelService.getOne(wrapper).getHotelName());
        //获取酒店库存
        QueryWrapper<ItripHotelTempStore> wrapper2 = new QueryWrapper<>();
        wrapper2.ge(vrsv.getCheckInDate()!=null,"recordDate", vrsv.getCheckInDate());
        wrapper2.le(vrsv.getCheckInDate()!=null,"recordDate", vrsv.getCheckOutDate());
        List<ItripHotelTempStore> list = iItripHotelTempStoreService.list(wrapper2);
        if(list.size()<=0){
            return DtoUtil.returnError("100512  ", "暂时无房", "");
        }
        //库存集合
        List<Integer> minNum = new ArrayList<>();
        for (ItripHotelTempStore it:
        list) {
            minNum.add(it.getStore());
        }
        //存入最小库存
        roomStoreVO.setStore(Collections.min(minNum));
        System.out.println("最小库存是"+Collections.min(minNum));

        //计算酒店价格
        QueryWrapper<ItripHotelRoom> wrapper3 = new QueryWrapper<>();
        wrapper3.eq(vrsv.getRoomId()!=null,"id", vrsv.getRoomId());
        BigDecimal roomPrice = iItripHotelRoomService.getOne(wrapper3).getRoomPrice();
        System.out.println("单酒店价格"+roomPrice);
        roomStoreVO.setPrice(BigDecimal.valueOf(roomPrice.doubleValue()*vrsv.getCount()));
        System.out.println("总价格"+BigDecimal.valueOf(roomPrice.doubleValue()*vrsv.getCount()));


        return DtoUtil.returnSuccess("成功", roomStoreVO);
    }

    @GetMapping("/api/hotelorder/queryOrderById/{orderId}")
    public Dto queryOrderById(@PathVariable("orderId") Integer orderId) {
        System.out.println(">>> 根据订单ID获取订单信息");
        System.out.println("订单ID:" + orderId);
        //返回ItripModifyHotelOrderVO
        QueryWrapper<ItripHotelOrder> wrapper = new QueryWrapper<>();
        wrapper.eq(orderId != null, "id", orderId);
        ItripHotelOrder one = iItripHotelOrderService.getOne(wrapper);

        Mapper mapper = new DozerBeanMapper();
        //实体类和vo转换映射
        ItripModifyHotelOrderVO itripModifyHotelOrderVO = mapper.map(one, ItripModifyHotelOrderVO.class);
        //根据名字查询linkUserId
        String [] arr = one.getLinkUserName().split(",");
        System.out.println("联系人数组"+ Arrays.toString(arr));

        //根据名字查询联系人id
        List<ItripOrderLinkUserVo> linksUserList = new ArrayList<>();
        for (String temp:
        arr) {
            QueryWrapper<ItripUserLinkUser> wrapper2 = new QueryWrapper<>();
            wrapper2.eq(temp != null, "linkUserName", temp);
            ItripUserLinkUser link = itripUserLinkUserService.getOne(wrapper2);

            ItripOrderLinkUserVo iov = new ItripOrderLinkUserVo();
            iov.setLinkUserId(link.getId());
            iov.setLinkUserName(temp);

            linksUserList.add(iov);
        }

        itripModifyHotelOrderVO.setItripOrderLinkUserList(linksUserList);

        return DtoUtil.returnSuccess("成功", itripModifyHotelOrderVO);
    }

    //分页测试
    @GetMapping("/queryUser")
    public PageAssistant<ItripHotelOrder> queryList(ItripHotelOrder iho,PageAssistant<ItripHotelOrder> pa) {
        /**
         * 这些代码应该写在service层
         */
//        //总记录
//        private Long total;
//        //总页数
//        private Long pageCount;
//        //当前页
//        private Integer current;
//        //每页显示
//        private Integer size;
//
//        /* 页面内容集合 */
//        private List<T> items;
        System.out.println("当前页"+pa.getCurPage()+"每页个数"+pa.getPageSize());

        QueryWrapper<ItripHotelOrder> wrapper = new QueryWrapper<>();
        wrapper.eq(iho.getHotelName()!=null,"hotelName", iho.getHotelName());
        wrapper.eq(iho.getId()!=null,"id", iho.getId());

        IPage<ItripHotelOrder> page = new Page<>(pa.getCurPage(), pa.getPageSize());
        pa.setRows(iItripHotelOrderService.page(page, wrapper).getRecords());
        pa.setPageSize((int) page.getSize());
        pa.setTotal((int) page.getTotal());

//        GetpersonalorderlistVO vo = new GetpersonalorderlistVO();
//        IPage<ItripHotelOrder> page = new Page<>(current, size);
//
//        IPage<ItripHotelOrder> page1 = iItripHotelOrderService.page(page, null);
//        System.out.println("测试"+page1.getPages());
//
//
//        vo.setCurrent(current);
//        vo.setSize(size);
//        vo.setTotal(page1.getTotal());
//        vo.setItripHotelOrderList(page1.getRecords());
        return pa;
    }
}
